/**
 * 用户管理 - 角色授权
 * @author wenchuan.li
 * @date 2018/12/15 22:08
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            form: '',
            list: []
        },
        callObj,
        rolesData = null,
        data = null,
        table
    ;

    var UserRoles = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'user_roles'
            }, win);
        },
        roles: function (fn) {
            //所有角色(不是左边表格的查询)
            // console.log(fn);
            app.ajax({
                xurl: 'sys/role/getEnableRoles',
                cb: function (result) {
                    if (result.ok) {
                        rolesData = result.data.data;
                        fn && fn();
                    }
                }
            });
        },
        initRolesTable: function () {
            table.render({
                elem: app.getByid(doc, 'roles_table'),
                id: 'roles_table',
                cols: [[
                    {field: 'ID', type: 'checkbox', title: '选择'},
                    {field: 'NAME', width: 100, title: '角色名称'},
                    {field: 'REMARK', minWidth: 150, title: '备注'}
                ]],
                page: {layout: ['prev', 'page', 'next', 'count']}
            });
        },
        tableReload: function () {
            table.reload('roles_table', {//待选角色
                data: data.filterKV({has: 0})
            });
            table.reload('usrroles_table', {//已选角色
                data: data.filterKV({has: 1})
            });
        },
        initUserRolesTable: function () {
            table.render({
                elem: app.getByid(doc, 'usrroles_table'),
                id: 'usrroles_table',
                size: _layuiConfig.ui_size,
                cols: [[
                    {field: 'ID', type: 'checkbox', title: '选择'},
                    {field: 'NAME', width: 100, title: '角色名称'},
                    {field: 'REMARK', title: '备注'}
                ]],
                page: {layout: ['prev', 'page', 'next', 'count']}
            });
        },
        rolesByUserId: function () {
            //右边用户已分配角色
            data = rolesData;
            callObj.data.TYPE=(callObj.data.TYPE || '');
            data.forEach(function (v) {
               if(v.TYPE != callObj.data.TYPE){
                   data.removeByMap({
                       TYPE: v.TYPE
                   });
               }
            });
            data.addMap({has: 0}, true);
            app.ajax({
                xurl: 'sys/user/listUserRoles',
                xdata: {
                    userId: callObj.data.ID
                },
                cb: function (result) {
                    if (result.ok) {
                        result.data.data.forEach(function (v) {
                            data.filterKV({ID: v.ROLE_ID}).addMap({has: 1}, true);
                        });
                        self.tableReload();

                    }
                }
            });
        },
        _call: function (obj) {
            callObj = obj;
            self.roles();
            if (!rolesData) {
                self.roles(function () {
                    self.rolesByUserId();
                });
            } else {
                self.rolesByUserId();
            }
        },
        _ready: function () {
            self.roles();
            table = layui.table;
            new Vue({
                el: '#user_roles',
                data: vueData,
                methods: {
                    addRole: function () {
                        var checkStatus = table.checkStatus('roles_table');
                        if (checkStatus.data.length > 0) {
                            var _xdata = [];
                            checkStatus.data.forEach(function (v) {
                                var _temp = app.fields_('sys_user_role');
                                _temp.USER_ID = callObj.data.ID;
                                _temp.ROLE_ID = v.ID;
                                _xdata.push(_temp);
                            });
                            app.ajax({
                                xurl: 'sys/user/addUserRoles',
                                xdata: _xdata,
                                loading: true,
                                toJsonStr: true,
                                okmsg: '用户角色添加成功',
                                cb: function (result) {
                                    if (result.ok) {
                                        result.xdata.forEach(function (v) {
                                            data.filterKV({ID: v.ROLE_ID}).addMap({has: 1}, true);
                                        });
                                        self.tableReload();
                                        app.pageModule('user').reloadTableCurr();
                                    }
                                }
                            });
                        }
                    },
                    delRole: function () {
                        var checkStatus = table.checkStatus('usrroles_table');
                        if (checkStatus.data.length > 0) {
                            var _xdata = [];
                            checkStatus.data.forEach(function (v) {
                                var _temp = app.fields_('sys_user_role');
                                _temp.USER_ID = callObj.data.ID;
                                _temp.ROLE_ID = v.ID;
                                _xdata.push(_temp);
                            });
                            app.ajax({
                                xurl: 'sys/user/delUserRoles',
                                xdata: _xdata,
                                loading: true,
                                toJsonStr: true,
                                okmsg: '用户角色移除成功',
                                cb: function (result) {
                                    if (result.ok) {
                                        result.xdata.forEach(function (v) {
                                            data.filterKV({ID: v.ROLE_ID}).addMap({has: 0}, true);
                                        });
                                        self.tableReload();
                                        app.pageModule('user').reloadTableCurr();
                                    }
                                }
                            });
                        }
                    },
                    sure: function () {
                        app.ajax({
                            xurl: 'sys/user/edit',
                            xdata: vueData.form,
                            loading: true,
                            okmsg: '用户信息保存成功',
                            cb: function (result) {
                                if (result.ok) {
                                    app.pageModule('user').reloadTableCurr();
                                    layer.close(layer.index);
                                }
                            }
                        });
                    }
                },
                watch: {},
                created: function () {
                    self._setEleIsReady('user_roles')
                }
            });
            self.initRolesTable();
        },
        _lazy: function () {
            this._super();
            self.initUserRolesTable();
        }
    });

    return new UserRoles();

});
